package com.magic.himalaya.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

/**
 * @description: 自定义圆润ImageView
 * @create: Create by Magic on 2022/1/21.
 */
public class RoundRectImageView extends AppCompatImageView {
    private static final String TAG = RoundRectImageView.class.getSimpleName();
    private float roundRatio = 0.1f;
    private Path path;

    public RoundRectImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onDraw(Canvas canvas) {
        if (path == null){
            path = new Path();
            path.addRoundRect(
                    new RectF(0,0,getWidth(),getHeight()),
                    roundRatio*getWidth(),
                    roundRatio*getHeight(),
                    Path.Direction.CW
            );
        }
        canvas.save();
        canvas.clipPath(path);
        super.onDraw(canvas);
        canvas.restore();
    }
}
